package thread.relyon;

/**
 * main线程和子线程的依赖关系
 */
public class RelyOn {


    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(() -> {
            while (true) {
                try {
                    Thread.sleep(1000);
                    System.out.println("child execute");
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }

        });

        thread.setName("child");
        thread.start();

        // 调用loop，main线程会一直存在；不调用loop，main线程执行完就退出了，而子线程会一直存在
        loop();

    }

    public static void loop() throws InterruptedException {
        while (true) {
            Thread.sleep(2000);
            System.out.println("main execute");
        }
    }

}
